<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/film_download.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/filmAction.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/spokenLanguageAction.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/writtenLanguageAction.php');
class filmDownloadAction
{
	private $film_download;
	private $filmAction;
	private $spokenLanguageAction;
	private $writtenLanguageAction;
	function __construct()
	{
		$this->film_download = new film_download();
		$this->filmAction = new filmAction();
	}
	public function AddFilmDownload($data)
	{
		try
		{
			/*if(isset($data['file_url_ed2k']))
			{
				$a = explode('|',$data['file_url_ed2k']);
				$data['file_size'] = $a[3];
				$data['file_name'] = urldecode($a[2]);
			}*/
			$data['file_url_ed2k'] = str_replace("\r"," ",$data['file_url_ed2k']);
			$data['file_url_ed2k'] = str_replace("\n"," ",$data['file_url_ed2k']);
			$data['file_url_ed2k'] = str_replace("  "," ",$data['file_url_ed2k']);
			if(!isset($data['film_id'])||empty($data['film_id']))
			{
				$data['film_id'] = $this->filmAction->getFilmIdByDoubanId($data['douban_id']);
			}
			if(!isset($data['runtime'])||empty($data['runtime']))
			{
				$data['runtime'] = intval($data['runtime_hour'])*3600 + intval($data['runtime_minute'])*60 + intval($data['runtime_second']);
			}
			return $this->film_download->insert($data);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
	}
	
	public function getFilmDownloadByFilmId($film_id,$column='*',$written_language='zh-Hans-CN')
	{
		try
		{
			$download_info = $this->film_download->getByFilmId($film_id,$column);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$this->spokenLanguageAction = new spokenLanguageAction();
			$this->writtenLanguageAction = new writtenLanguageAction();
			foreach($download_info as $key=>$value)
			{
				$download_info[$key]['audio_language'] = $this->spokenLanguageAction->getNameByIdSum($value['spoken_language_id_sum'],$written_language);
				$download_info[$key]['video_language'] = $this->writtenLanguageAction->getOriginalNameByIdSum($value['written_language_id_sum'],$written_language);
			}
			return $download_info;
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
	}
}

?>
